﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件名：EmployeesManage.cs
  //文件功能描述：员工管理
  //创建人：赵国伟
  //生成时间：2012/03/17 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class EmployeesManage
    {
        private readonly DALEmployees dalEmployee = new DALEmployees();

        #region "保存一条员工信息 by 赵国伟 2012-03-17"
        /// <summary>
        /// 保存一条员工信息
        /// </summary>
        /// <param name="employeeInfo"></param>
        /// <param name="roleIds"></param>
        /// <returns></returns>
        public ReturnValue Save(Employees employeeInfo, string roleIds)
        {
            ReturnValue ret = new ReturnValue();
            if (employeeInfo.EmployeeID == 0)
            {
                //判断登录名是否重复；
                if (dalEmployee.QueryList(1, "EmployeeName='" + employeeInfo.EmployeeName + "'", "") != null)
                {
                    ret.Message = "您输入的登录名已存在！";
                    return ret;
                }
                employeeInfo.PassWord = Utility.MD5(employeeInfo.PassWord);
                employeeInfo.LastLoginDate = DateTime.Now;
                employeeInfo.CreateDate = DateTime.Now;
            }
            else
            {
                //判断用户名是否重复；
                if (dalEmployee.QueryList(1, "EmployeeName='" + employeeInfo.EmployeeName + "' and EmployeeID <>" + employeeInfo.EmployeeID + "", "") != null)
                {
                    ret.Message = "您输入的用户名已存在！";
                    return ret;
                }

                Employees oldInfo = dalEmployee.GetModel(employeeInfo.EmployeeID);
                if (oldInfo == null)
                    ret.Message = "编辑的信息不存在！";
                else
                {
                    if (employeeInfo.PassWord == "")
                        employeeInfo.PassWord = oldInfo.PassWord;
                    else
                        employeeInfo.PassWord = Utility.MD5(employeeInfo.PassWord);
                    employeeInfo.Logins = oldInfo.Logins;
                    employeeInfo.LastLoginIP = oldInfo.LastLoginIP;
                    employeeInfo.LastLoginDate = oldInfo.LastLoginDate;
                    employeeInfo.CreateDate = oldInfo.CreateDate;
                }

            }
            return dalEmployee.SaveEmployee(employeeInfo, roleIds);
        }
        #endregion

        #region "得到一个员工对象 by 赵国伟 2012-03-17"
        /// <summary>
        /// 得到一个员工对象
        /// </summary>
        /// <param name="EmployeeID"></param>
        /// <returns></returns>
        public EmployeeInfo GetEmployee(int EmployeeID)
        {
            Employees e = dalEmployee.GetModel(EmployeeID);
            EmployeeInfo data = null;
            if (e != null)
            {
                DALPurviewRole dalRoles = new DALPurviewRole();
                data = new EmployeeInfo();
                data.Employee = e;
                data.Roles = dalRoles.QueryListToBase(EmployeeID);
            }
            return data;
        }
        #endregion

        #region "更新指定员工的状态； by 赵国伟 2012-03-17"
        /// <summary>
        /// 更新指定员工的状态；
        /// </summary>
        /// <param name="employeeId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public ReturnValue UpdateStatus(int employeeId, Status status)
        {
            ReturnValue ret = new ReturnValue();
            if (DALHelper.SetTableStatus<Employees>(employeeId, status.ToString()) > 0)
                ret.Message = "操作失败！";
            else
            {
                ret.Result = ResultType.Success;
                ret.Message = "操作成功！";
            }
            return ret;
        }
        #endregion

        #region "获得前几行数据 返回Model对象列表 by 赵国伟 2012-03-17"
        /// <summary>
        ///  根据指定条件及排序字段返回指定数量的Model对象；
        /// </summary>
        /// <param name=	op">0为返回全部</param>
        /// <param name="strWhere">条件语句不带Where</param>
        /// <param name="filedOrder">排序字段 不带Orderby</param>
        /// <returns></returns>
        public List<Shopping.Model.Employees> QueryList(int top, string strWhere, string filedOrder)
        {
            return dalEmployee.QueryList(top, strWhere, filedOrder);
        }
        public List<Shopping.Model.Employees> QueryList(string strWhere, string filedOrder)
        {
            return dalEmployee.QueryList(0, strWhere, filedOrder);
        }
        #endregion

        #region "分页获取数据列表 返回结果为Model列表 by 赵国伟 2012-03-17"
        /// <summary>
        ///  分页获取数据列表 返回结果为Model列表；
        /// </summary>
        /// <param name="strWhere">条件语句不带Where</param>
        /// <param name="filedOrder">排序字段 不带Orderby</param>
        /// <param name="curPage">当前页</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="totalCount">记录总数 out型参数</param>
        /// <returns></returns>
        public List<Shopping.Model.Employees> QueryListAsPage(string strWhere, string filedOrder,
             int curPage, int pageSize, out int totalCount)
        {
            return dalEmployee.QueryListAsPage(strWhere, filedOrder, curPage, pageSize, out  totalCount);
        }
        #endregion

        #region "通过用户名查找指定的员工信息 by 赵国伟 2012-03-17"
        /// <summary>
        /// 通过用户名查找指定的员工信息；
        /// </summary>
        /// <param name="employeeName"></param>
        /// <returns></returns>
        public Employees GetUserInfoByName(string employeeName)
        {
            List<Employees> listOfEmployees = dalEmployee.QueryList(1,"EmployeeName='" + employeeName + "'", "");
            if (listOfEmployees != null)
                return listOfEmployees[0];
            else
                return null;
        }
        #endregion

        #region "设置登录信息 by 赵国伟 2012-03-17"
        /// <summary>
        /// 设置登录信息
        /// </summary>
        public void Login(Employees e)
        {
            if (e == null) return;

            Utility.SetCookies(CConst.Cookie_Employee_ID, e.EmployeeID.ToString(), "", 0);
            Utility.SetCookies(CConst.Cookie_Employee_Name, e.EmployeeName, "", 0);
            Utility.SetCookies(CConst.Cookie_Employee_TrueName, System.Web.HttpUtility.UrlEncode(e.TrueName), "", 0);
            Utility.SetCookies(CConst.Cookie_Employee_Logins, (e.Logins + 1).ToString(), "", 0);
            Utility.SetCookies(CConst.Cookie_Employee_LastLoginDate, e.LastLoginDate.ToString(), "", 0);
            Utility.SetCookies(CConst.Cookie_Employee_LastLoginIP, Utility.GetClientIPAddress(), "", 0);
            e.Logins++;
            e.LastLoginDate = DateTime.Now;
            e.LastLoginIP = Utility.GetClientIPAddress();
            dalEmployee.Update(e);
        }
        #endregion

        #region "设置登出信息；by 赵国伟 2012-03-17"
        /// <summary>
        /// 设置登出信息；by 赵国伟 2012-03-17
        /// </summary>
        public void Logout()
        {
            Utility.SetCookies(CConst.Cookie_Employee_ID, "", "", 0);
            Utility.SetCookies(CConst.Cookie_Employee_Name, "", "", 0);
            Utility.SetCookies(CConst.Cookie_Employee_TrueName, "", "", 0);
            Utility.SetCookies(CConst.Cookie_Employee_Logins, "", "", 0);
            Utility.SetCookies(CConst.Cookie_Employee_LastLoginDate, "", "", 0);
            Utility.SetCookies(CConst.Cookie_Employee_LastLoginIP, "", "", 0);
        }
        #endregion

        #region "判断是否处于登录状态 by 赵国伟 2012-03-17"
        /// <summary>
        /// 判断是否处于登录状态
        /// </summary>
        /// <returns></returns>
        public bool IsLogin()
        {
            if (Utility.GetCookies(CConst.Cookie_Employee_ID) == "")
                return false;
            else
                return true;
        }
        #endregion

        #region "获取当前已登录用户的状态信息； by 赵国伟 2012-03-17"
        /// <summary>
        /// 获取当前已登录用户的状态信息；
        /// </summary>
        /// <returns></returns>
        public Employees GetLoginStatus()
        {
            Employees e = new Employees();
            e.EmployeeID = Utility.Cast2int(Utility.GetCookies(CConst.Cookie_Employee_ID));
            e.EmployeeName = Utility.GetCookies(CConst.Cookie_Employee_Name);
            e.TrueName = Utility.GetCookies(CConst.Cookie_Employee_TrueName);
            e.Logins = Utility.Cast2int(Utility.GetCookies(CConst.Cookie_Employee_Logins));
            e.LastLoginDate = Utility.Cast2datetime(Utility.GetCookies(CConst.Cookie_Employee_LastLoginDate));
            e.LastLoginIP = Utility.GetCookies(CConst.Cookie_Employee_LastLoginIP);
            return e;
        }
        #endregion

        #region "从Cookies中 获取当前用户的ID; by赵国伟 2012-03-26"
        /// <summary>
        /// 从Cookies中 获取当前用户的ID;
        /// </summary>
        /// <returns></returns>
        public int GetEmployeeIDFromCookies()
        {
            return Utility.Cast2int(Utility.GetCookies(CConst.Cookie_Employee_ID));
        }
        #endregion





    }
}
